//
//  YMBaseTableViewController.m
//  PinGJByOwnerSide
//
//  Created by lantaiyuan on 2017/10/13.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

#import "YMBaseTableViewController.h"
#import "YMNavigationBar.h"

@interface YMBaseTableViewController ()

@end

@implementation YMBaseTableViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
}


- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    self.navigationController.navigationBar.hidden = YES;
    [self.navigationBar removeFromSuperview];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = CONTENT_BG_COLOR;
    [self.navigationController.navigationBar addSubview:self.navigationBar];
    self.navigationBar.backgroundColor = GREEN_COLOR;
}

#pragma mark - 导航栏

- (YMNavigationBar *)navigationBar{
    if (!_navigationBar) {
//        if (iPhone8_X) {
//            _navigationBar = [[YMNavigationBar alloc] initWithFrame:CGRectMake(0, -20, SCREEN_WIDTH, 88)];
//        } else {
//            _navigationBar = [[YMNavigationBar alloc] initWithFrame:CGRectMake(0, -20, SCREEN_WIDTH, 64)];
//        }
        _navigationBar = [[YMNavigationBar alloc] initWithFrame:self.navigationController.navigationBar.bounds];
        _navigationBar.title = self.navigationItem.title;
    }
    return _navigationBar;
}

#pragma mark - 标准首页导航栏(已和UI确认,暂时用安卓的标注图 20 + 48)

#pragma mark - 右导航栏按钮

/**
 添加右导航栏按钮
 
 @param image 图片
 @param click 点击回调
 */
- (void)rightNavButtonWithImage:(UIImage *)image action:(void(^)(void))click{
    
    UIImageView * rightImageView = [[UIImageView alloc] init];
    rightImageView.image = image;
    rightImageView.contentMode = UIViewContentModeCenter;
    [self.navigationBar addSubview:rightImageView];
    
    self.dotImageView = [[UIImageView alloc]init];
    self.dotImageView.image = [UIImage imageNamed:@"icon_spot"];
    self.dotImageView.contentMode = UIViewContentModeCenter;
    [self.navigationBar addSubview:self.dotImageView];
    self.dotImageView.hidden = YES;
    UIButton * btn = [[UIButton alloc] init];
    [self.navigationBar addSubview:btn];
    
    WEAKSELF
    if (iPhone8_X) {
        [rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf.navigationBar.mas_right).offset(-15);
            make.top.equalTo(@55);
            make.width.equalTo(@22);
            make.height.equalTo(@22);
        }];
        
        [self.dotImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(rightImageView.mas_right).offset(-0.5);
            make.top.equalTo(rightImageView.mas_centerY).offset(-10);
            make.width.equalTo(@6);
            make.height.equalTo(@6);
        }];
        
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf.navigationBar.mas_right).offset(0);
            make.top.equalTo(@44);
            make.width.equalTo(@52);
            make.height.equalTo(@44);
        }];
    } else {
        [rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf.navigationBar.mas_right).offset(-15);
            make.top.equalTo(@31);
            make.width.equalTo(@22);
            make.height.equalTo(@22);
        }];
        
        [self.dotImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(rightImageView.mas_right).offset(-0.5);
            make.top.equalTo(rightImageView.mas_centerY).offset(-10);
            make.width.equalTo(@6);
            make.height.equalTo(@6);
        }];
        
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf.navigationBar.mas_right).offset(0);
            make.top.equalTo(@20);
            make.width.equalTo(@52);
            make.height.equalTo(@44);
        }];
    }
    [btn jk_touchDown:^{
        click();
    }];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
}
/**
 添加右导航栏按钮
 
 @param title 标题
 @param click 点击回调
 */
- (void)rightNavButtonWithTitle:(NSString *)title action:(void(^)(void))click{
    UIButton * btn = [[UIButton alloc] init];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor colorWithHexString:@"ffffff"] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.navigationBar addSubview:btn];
    
    CGFloat width = [NSString boundingRectWithString:title font:btn.titleLabel.font].width;
    
    WEAKSELF
    if (iPhone8_X) {
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf.navigationBar.mas_right).offset(-15);
            make.top.equalTo(@44);
            make.width.equalTo(@(width + 0.25));
            make.height.equalTo(@44);
        }];
    } else {
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf.navigationBar.mas_right).offset(-15);
            make.top.equalTo(@20);
            make.width.equalTo(@(width + 0.25));
            make.height.equalTo(@44);
        }];
    }
    [btn jk_touchDown:^{
        click();
    }];
}

/**
 添加右导航栏按钮
 
 @param title 标题
 @param color 颜色
 @param click 点击回调
 */
- (void)rightNavButtonWithTitle:(NSString *)title color:(UIColor *)color action:(void(^)(void))click{
    UIButton * btn = [[UIButton alloc] init];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:color forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.navigationBar addSubview:btn];
    
    CGFloat width = [NSString boundingRectWithString:title font:btn.titleLabel.font].width;
    
    WEAKSELF
    if (iPhone8_X) {
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf.navigationBar.mas_right).offset(-15);
            make.top.equalTo(@44);
            make.width.equalTo(@(width + 0.25));
            make.height.equalTo(@44);
        }];
    } else {
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf.navigationBar.mas_right).offset(-15);
            make.top.equalTo(@20);
            make.width.equalTo(@(width + 0.25));
            make.height.equalTo(@44);
        }];
    }
    [btn jk_touchDown:^{
        click();
    }];
}

//标准版首页导航栏添加右导航按钮(切换城市)
- (void)travelRightNavButtonWithTitle:(NSString *)title color:(UIColor *)color imageName:(NSString *)imageName action:(void(^)(void))click{
    
    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.image = [UIImage imageNamed:imageName];
    [self.navigationBar addSubview:imageView];
    
    WEAKSELF
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.navigationBar.mas_right).offset(-15);
        make.top.equalTo(weakSelf.navigationBar.mas_top).offset(39.5);
    }];
    
    UIButton *btn = [[UIButton alloc]init];
    
    if (title.length >= 4) {
        title = [title substringToIndex:4];
    }
    
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:color forState:UIControlStateNormal];
    
    if (title.length >= 4) {
        btn.titleLabel.font = [UIFont systemFontOfSize:10];
    }else if (title.length == 3){
        btn.titleLabel.font = [UIFont systemFontOfSize:13];
    }else{
        btn.titleLabel.font = [UIFont systemFontOfSize:19];
    }
    
    [self.navigationBar addSubview:btn];
    
    CGFloat btnWidth = [NSString boundingRectWithString:title font:btn.titleLabel.font].width;
    
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(imageView.mas_left).offset(-6);
        make.width.equalTo(@(btnWidth + 0.25));
        make.centerY.equalTo(imageView.mas_centerY);
    }];
    
    UIButton *coverBtn = [[UIButton alloc]init];
    [self.navigationBar addSubview:coverBtn];
    
    [coverBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(btn.mas_left).offset(0);
        make.right.equalTo(weakSelf.navigationBar.mas_right).offset(-15);
        make.centerY.equalTo(imageView.mas_centerY);
        make.height.equalTo(@44);
    }];
    
    [coverBtn jk_touchDown:^{
        click();
    }];
}

#pragma mark - 左导航栏按钮
/**
 添加左导航栏按钮
 
 @param image 图片
 @param click 点击回调
 */
- (void)leftNavButtonWithImage:(UIImage *)image action:(void (^)(void))click{
    UIImageView * leftNavImageView = [[UIImageView alloc] init];
    leftNavImageView.image = image;
    [self.navigationBar addSubview:leftNavImageView];
    
    UIButton * backBtn = [[UIButton alloc] init];
    [self.navigationBar addSubview:backBtn];
    
    WEAKSELF
    if (iPhone8_X) {
        [leftNavImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.navigationBar.mas_left).offset(10);
            make.top.equalTo(@55);
            make.width.equalTo(@22);
            make.height.equalTo(@22);
        }];
        if ([YMSaveTool isLogin]) {
            leftNavImageView.layer.cornerRadius = 11;
            leftNavImageView.layer.masksToBounds = YES;
        }
        [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.navigationBar.mas_left).offset(0);
            make.top.equalTo(@44);
            make.width.equalTo(@44);
            make.height.equalTo(@44);
        }];
    } else {
        [leftNavImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.navigationBar.mas_left).offset(10);
            make.top.equalTo(@31);
            make.width.equalTo(@22);
            make.height.equalTo(@22);
        }];
        if ([YMSaveTool isLogin]) {
            leftNavImageView.layer.cornerRadius = 11;
            leftNavImageView.layer.masksToBounds = YES;
        }
        [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.navigationBar.mas_left).offset(0);
            make.top.equalTo(@20);
            make.width.equalTo(@44);
            make.height.equalTo(@44);
        }];
    }
    backBtn.eventTimeInterval = 1.5;
    [backBtn jk_touchDown:^{
        click();
    }];
}

/**
 添加左导航栏按钮
 
 @param title 图片
 @param click 点击回调
 */
- (void)leftNavButtonWithTitle:(NSString *)title action:(void (^)(void))click{
    UIImageView * backImageView = [[UIImageView alloc] init];
    backImageView.image = [UIImage imageNamed:@"nav_button_return"];
    backImageView.userInteractionEnabled = YES;
    [self.navigationBar addSubview:backImageView];
    
    UILabel * label = [[UILabel alloc] init];
    label.text = title;
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:15];
    label.adjustsFontSizeToFitWidth = YES;
    [self.navigationBar addSubview:label];
    
    UIButton * backBtn = [[UIButton alloc] init];
    [self.navigationBar addSubview:backBtn];
    
    WEAKSELF
    if (iPhone8_X) {
        [backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.navigationBar.mas_left).offset(10);
            make.top.equalTo(@55);
            make.width.equalTo(@22);
            make.height.equalTo(@22);
        }];
        
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(backBtn.mas_right).offset(-20);
            make.top.equalTo(@55);
            make.width.equalTo(@80);
            make.height.equalTo(@22);
        }];
        
        [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.navigationBar.mas_left).offset(0);
            make.top.equalTo(@40);
            make.width.equalTo(@54);
            make.height.equalTo(@44);
        }];
    } else {
        [backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.navigationBar.mas_left).offset(10);
            make.top.equalTo(@31);
            make.width.equalTo(@22);
            make.height.equalTo(@22);
        }];
        
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(backBtn.mas_right).offset(-20);
            make.top.equalTo(@31);
            make.width.equalTo(@80);
            make.height.equalTo(@22);
        }];
        
        [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.navigationBar.mas_left).offset(0);
            make.top.equalTo(@20);
            make.width.equalTo(@54);
            make.height.equalTo(@44);
        }];
    }
    
    backBtn.eventTimeInterval = 1.5;
    [backBtn jk_touchDown:^{
        click();
    }];
}


/**
 添加左导航栏按钮(返回按钮)
 
 @param click 点击回调
 */
- (void)backButtonAction:(void (^)(void))click{
    
    UIButton * backBtn = [[UIButton alloc] init];
    [self.navigationBar addSubview:backBtn];
    
    UIImageView * backImageView = [[UIImageView alloc] init];
    backImageView.image = [UIImage imageNamed:@"nav_button_return"];
    [backBtn addSubview:backImageView];
    
    WEAKSELF
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.navigationBar.mas_left).offset(0);
        make.top.equalTo(@0);
        make.width.mas_equalTo(weakSelf.navigationBar.height);
        make.height.mas_equalTo(weakSelf.navigationBar.height);
    }];
    
    [backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(backBtn.center);
        make.width.equalTo(@20);
        make.height.equalTo(@20);
    }];
    
    backBtn.eventTimeInterval = 1.5;
    [backBtn jk_touchDown:^{
        click();
    }];
}

//标准版首页左导航按钮
- (void)travelLeftNavButtonAction:(void(^)(void))click{
    UIButton *btn = [[UIButton alloc]init];
    [btn setImage:[UIImage imageNamed:@"nav_button_return"] forState:UIControlStateNormal];
    [self.navigationBar addSubview:btn];
    
    UIButton *coverBtn = [[UIButton alloc]init];
    [self.navigationBar addSubview:coverBtn];
    WEAKSELF
    if (iPhone8_X) {
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.navigationBar.mas_left).offset(15);
            make.top.equalTo(@55);
            make.width.equalTo(@23);
            make.height.equalTo(@23);
        }];
    } else {
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.navigationBar.mas_left).offset(15);
            make.top.equalTo(@31);
            make.width.equalTo(@23);
            make.height.equalTo(@23);
        }];
    }
    [coverBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(btn.mas_left).offset(-15);
        make.right.equalTo(btn.mas_right).offset(15);
        make.centerY.equalTo(weakSelf.navigationBar.mas_centerY).offset(10);
        make.height.equalTo(@44);
    }];
    [coverBtn jk_touchDown:^{
        click();
    }];
}

@end
